﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel;
using MsgService;
using MvcContrib.Pagination;//用于Grid显示
using MvcContrib.UI.Grid;//用于Grid显示
using System.IO;
using WROA.Helper;
using WROA.WEB.Models;
using WROA.ActionFilter;

namespace WROA.Controllers
{
    public class SharedController : Controller
    {
        //
        // GET: /Shared/

        public ActionResult EditOnline(string name)
        {
            Session["fileName"] = name;
            string path = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Uploads/" + name;
            ViewData["strwordpath"] = path;
            return View();
        }

        public ActionResult SaveImage(HttpPostedFileBase file)
        {
            if (file == null)
            {
                ModelState.AddModelError("FileName", "文件名不能为空！");
                return View();
            }
            ///'检查文件扩展名字
            string fileName, fileExtension;
            fileName = System.IO.Path.GetFileName(file.FileName);
            if (fileName != "")
            {
                fileExtension = System.IO.Path.GetExtension(fileName);
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                string serverStr = Server.MapPath("~/UpLoadFile/") + DateTime.Now.ToString("yyyyMMdd") + fileExtension;//获取保存的文件夹路径
                file.SaveAs(serverStr);
            }
            return Content("true");
        }

        public void SendMsg()
        {
            //绑定
            NetTcpBinding tcpBind = new NetTcpBinding();
            //地址
            EndpointAddress address = new EndpointAddress("net.tcp://localhost:3200/sendMsg");
            //通道工厂
            ChannelFactory<IService> factory = new ChannelFactory<IService>(tcpBind, address);

            IService myObject = factory.CreateChannel();

            bool result = myObject.sengMsg("15813350363", "我去");

            if (result == true)
            {
                string s = "<script langurage='javascript'>alert('消息已经发到手机提醒！');</script>";
                Response.Write(s);
            }
            else
            {
                string s = "<script langurage='javascript'>alert('消息操作失败！选择重试或者取消！');</script>";
                Response.Write(s);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Upload(HttpPostedFileBase Uploadfile)
        {
            if (Uploadfile != null)
            {
                Uploadfile.SaveAs(Server.MapPath("~/UpLoadFile/") + Uploadfile.FileName);
            }
            return View();
        }

        public ActionResult Delete(String name)
        {
            System.IO.File.Delete(Server.MapPath("~/UpLoadFile/") + name);
            return View();
        }

        public FilePathResult DownLoad(String name)
        {
            string Id = RouteData.Values["Id"].ToString();
            string Path = Server.MapPath("~/UpLoadFile/");
            //string fileName = resource.GetFileName(Id);
            string fileLocation = Path + Id;
            return File(fileLocation, "text/plain", Id);
            //return File("~/UpLoadFile/" + name, "text/plain", Server.HtmlEncode(name));
        }

        [RoleAuthorize(RoleName = new string[] { "管理员" })]
        public ActionResult ChangeWord(int? page, FormCollection form)
        {
            
            //System.IO.File.Delete(ConfigPath);
            //这里返回一个集合后，直接追加到这-->
            FileRepository filerepository = new FileRepository();
            //string s = filerepository.ChangeWord("20120728.doc");
            var models = filerepository.GetAllFileDescription().AsPagination(page.GetValueOrDefault(1), 2);
           // Response.Write(s);
            //Response.End();响应结束的话，后面页面就不能够返回了。
            return View(models);
            //return RedirectToAction("Index", "Home");
        }

        public ActionResult Detail()
        {
            //int resourceID = Convert.ToInt32(RouteData.Values["Id"]);
            //Resource Resuce = resource.GetResourceByResourceID(resourceID);
            //return View(Resuce);
            FileRepository filerepository = new FileRepository();
            FileAttribute e = null;
            foreach (FileAttribute f in filerepository.GetAllFileDescription())
            {
                e = f;
            }
            return View(e);
        }
    }
}
